package io.github.coderlaoliu.panels;

import io.github.coderlaoliu.encrypt.EncryptionStrategy;
import io.github.coderlaoliu.encrypt.EncryptionStrategyFactory;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.components.panels.VerticalLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EncryptPanel extends JPanel {
    private JTextArea inputTextArea;
    private JTextArea outputTextArea;
    private JTextField keyTextField;
    private JRadioButton aesRadioButton;
    private JRadioButton desRadioButton;
    private JRadioButton rc4RadioButton;
    private JRadioButton rabbitRadioButton;
    private JRadioButton md5RadioButton;
    private JRadioButton tripleDesRadioButton;
    private JButton encryptButton;
    private JButton decryptButton;

    public EncryptPanel() {
        initUI();
        initListeners();
    }

    private void initListeners() {
        encryptButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取上段文本域的输入内容
                String plainText = inputTextArea.getText();
                // 获取选中的加密算法
                String encryptionAlgorithm = getSelectedAlgorithm();
                // 获取密钥
                String key = keyTextField.getText();

                // 检查是否输入了明文
                if (plainText == null || plainText.isEmpty()) {
                    JOptionPane.showMessageDialog(EncryptPanel.this, "请输入明文", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (encryptionAlgorithm == null || encryptionAlgorithm.isEmpty()) {
                    JOptionPane.showMessageDialog(EncryptPanel.this, "请选择加密算法", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 检查是否选择了基于私钥加密的算法并且未输入密钥
                if (requiresPrivateKey(encryptionAlgorithm) && key.isEmpty()) {
                    JOptionPane.showMessageDialog(EncryptPanel.this, "请输入私钥", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 执行加密逻辑
                String encryptedText = encryptText(plainText, encryptionAlgorithm, key);

                // 将加密结果输出到下段文本框
                outputTextArea.setText(encryptedText);
            }
        });

        decryptButton.addActionListener(e -> {
            // 获取输入内容和选项
            String outputText = outputTextArea.getText();
            String algorithm = getSelectedAlgorithm();
            String key = keyTextField.getText();

            // 参数校验
            if (outputText == null || outputText.isEmpty()) {
                JOptionPane.showMessageDialog(EncryptPanel.this, "请输入待解密的密文", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (algorithm == null || algorithm.isEmpty()) {
                JOptionPane.showMessageDialog(EncryptPanel.this, "请选择算法", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (requiresPrivateKey(algorithm) && (key == null || key.isEmpty())) {
                JOptionPane.showMessageDialog(EncryptPanel.this, "请输入私钥", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            // 解密操作
            try {
                // 根据算法获取解密策略
                EncryptionStrategy strategy = EncryptionStrategyFactory.createEncryptionStrategy(algorithm);

                // 解密
                String decryptedText = strategy.decrypt(outputText, key);

                // 显示解密结果
                inputTextArea.setText(decryptedText);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(EncryptPanel.this, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
        });

    }

    // 检查是否选择了基于私钥加密的算法
    private boolean requiresPrivateKey(String encryptionAlgorithm) {
        return encryptionAlgorithm.equals("AES") || encryptionAlgorithm.equals("DES") || encryptionAlgorithm.equals("TripleDES");
    }

    private String getSelectedAlgorithm() {
        if (aesRadioButton.isSelected()) {
            return "AES";
        } else if (desRadioButton.isSelected()) {
            return "DES";
        } else if (rc4RadioButton.isSelected()) {
            return "RC4";
        } else if (rabbitRadioButton.isSelected()) {
            return "Rabbit";
        } else if (md5RadioButton.isSelected()) {
            return "MD5";
        } else if (tripleDesRadioButton.isSelected()) {
            return "TripleDES";
        }

        return null;
    }

    private String encryptText(String plainText, String algorithm, String key) {
        EncryptionStrategy strategy = EncryptionStrategyFactory.createEncryptionStrategy(algorithm);
        try {
            return strategy.encrypt(plainText, key);
        } catch (Exception e) {
            return "无法加密：" + e.getMessage();
        }
    }


    private void initUI() {
        setLayout(new VerticalLayout(10));

        // 上段文本域
        JLabel topLabel = new JLabel("在此处输入明文，选择对应的加密方式，也可以加入密钥，然后加密即可。");
        add(topLabel);

        inputTextArea = new JTextArea(10, 50);
        JScrollPane inputScrollPane = new JBScrollPane(inputTextArea);
        add(inputScrollPane);

        // 中间的单选框
        aesRadioButton = new JRadioButton("AES", true);
        desRadioButton = new JRadioButton("DES");
        rc4RadioButton = new JRadioButton("RC4");
        rabbitRadioButton = new JRadioButton("Rabbit");
        md5RadioButton = new JRadioButton("MD5");
        tripleDesRadioButton = new JRadioButton("TripleDES");

        ButtonGroup encryptionAlgorithmGroup = new ButtonGroup();
        encryptionAlgorithmGroup.add(aesRadioButton);
        encryptionAlgorithmGroup.add(desRadioButton);
        encryptionAlgorithmGroup.add(rc4RadioButton);
        encryptionAlgorithmGroup.add(rabbitRadioButton);
        encryptionAlgorithmGroup.add(md5RadioButton);
        encryptionAlgorithmGroup.add(tripleDesRadioButton);

        JPanel radioButtonPanel = new JPanel(new GridLayout(2, 3, 10, 10));
        radioButtonPanel.add(aesRadioButton);
        radioButtonPanel.add(desRadioButton);
        radioButtonPanel.add(rc4RadioButton);
        radioButtonPanel.add(rabbitRadioButton);
        radioButtonPanel.add(md5RadioButton);
        radioButtonPanel.add(tripleDesRadioButton);

        add(radioButtonPanel);

        // 密钥输入框
        JLabel keyLabel = new JLabel("在此输入密钥（可选）");
        keyTextField = new JTextField(20);
        JPanel keyPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        keyPanel.add(keyLabel);
        keyPanel.add(keyTextField);
        add(keyPanel);

        // 加密和解密按钮
        encryptButton = new JButton("加密");
        decryptButton = new JButton("解密");
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(encryptButton);
        buttonPanel.add(decryptButton);
        add(buttonPanel);

        // 下段文本域
        JLabel bottomLabel = new JLabel("加密后的密文会显示在这里。也可以是待解密的密文。");
        add(bottomLabel);

        outputTextArea = new JTextArea(10, 50);
        JScrollPane outputScrollPane = new JBScrollPane(outputTextArea);
        add(outputScrollPane);
    }
}
